package com.ibeeking.gateway.b.config.limiting;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

/**
 * @ClassName CurrentLimitConfig
 * @Description
 * @Author ibeeking
 * @Date 2020-08-31 11:57
 **/

@Configuration
public class CurrentLimitConfig {

    /***
     * 使用请求ip限流
     * @return
     */
    @Primary
    @Bean
    public KeyResolver hostAddrKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
    }

    /**
     * 使用请求api限流
     *
     * @return
     */
    @Bean
    public KeyResolver apiKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }
}
